﻿using System.Diagnostics;
using System.Linq;
using Best.SmartCms.Config;
using Best.SmartCms.Core.Utility;
using Best.SmartCms.Dto;
using Microsoft.AspNetCore.Mvc;
using Best.SmartCms.Portal.Models;
using Best.SmartCms.Service;
using Microsoft.Extensions.Caching.Memory;

namespace Best.SmartCms.Portal.Controllers
{
    public class HomeController : Controller
    {
        private readonly ChannelGroupService _channelGroupService;
        private readonly ChannelService _channelService;
        private readonly NewsService _newsService;
        private readonly IMemoryCache _cache;
        private static string NewsListChannelId => ConfigurationManagerEx.AppSettings["NewsListChannelId"];
        private static string ProductListChannelId => ConfigurationManagerEx.AppSettings["ProductListChannelId"];
        private static string CompanyNewsId => ConfigurationManagerEx.AppSettings["CompanyNewsId"];

        public HomeController(IMemoryCache memoryCache, ChannelGroupService channelGroupService, ChannelService channelService, NewsService newsService)
        {
            _channelGroupService = channelGroupService;
            _channelService = channelService;
            _newsService = newsService;
            _cache = memoryCache;
        }

        [Html(Path ="wwwroot/index.html")]
        public IActionResult Index()
        {
            var model = new IndexModel();
            var newslist = _newsService.PageList(new NewsListQueryDto
            {
                PageIndex = 1,
                PageSize = 10,
                ChannelId = NewsListChannelId.ToLong()
            });

            model.NewsList = newslist.Data.Select(p => new NewsModel
            {
                Id = p.Id,
                ChannelId = p.ChannelId,
                ChannelName=p.ChannelName,
                ControllerName = p.ControllerName,
                CreatedTime = p.CreatedTime.GetValueOrDefault(),
                Title = p.Title
            }).ToList();

            var proudList = _newsService.PageList(new NewsListQueryDto
            {
                PageIndex = 1,
                PageSize = 3,
                ChannelId = ProductListChannelId.ToLong()
            });

            model.ProductList = proudList.Data.Select(p => new NewsModel
            {
                Id = p.Id,
                ChannelId = p.ChannelId,
                ChannelName = p.ChannelName,
                ControllerName = p.ControllerName,
                CreatedTime = p.CreatedTime.GetValueOrDefault(),
                Title = p.Title,
                Summer = p.Summer,
                ImageUrl = p.ImageUrl

            }).ToList();


            var companyInfo = _newsService.Detail(CompanyNewsId.ToLong());
            model.CompanyInfo = new NewsModel
            {
                Id = companyInfo.Id,
                ChannelId = companyInfo.ChannelId,
                ChannelName = companyInfo.ChannelName,
                ControllerName = companyInfo.ControllerName,
                CreatedTime = companyInfo.CreatedTime.GetValueOrDefault(),
                Title = companyInfo.Title,
                Summer = companyInfo.Summer,
                ImageUrl = companyInfo.ImageUrl
            };

            return View(model);
        }

        

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
